package com.ruoyi.aitafk.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aitcommon.anotation.ServiceLog;
import com.ruoyi.aitcommon.service.SQLService;
import com.ruoyi.aitu9c.service.U9RestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class QJU9Service {
	@Autowired
	U9RestService u9rest;
	@Autowired
	SQLService sqlService;
	
	@ServiceLog("u9单据批量审批")
	public List<JSONObject> batchApprove(List<Map<String, Object>> datas
			,JSONObject context, Map<String,Object> urls, JSONObject entityInfo) throws Exception {
		
		Map<String,Object> datamap=new LinkedHashMap<String,Object>();
		//依据docname分组
		for(Map<String,Object> data:datas) {
			String docname=StrUtil.toString(data.get("docname"));
			String status=StrUtil.toString(data.get("status"));
			String key=docname+"-"+status;
			
			List<Map<String,Object>> datalist=null;
			if(datamap.containsKey(key)) {
				datalist=(List<Map<String,Object>>)datamap.get(key);
			}else {
				datalist=new ArrayList<Map<String,Object>>();
			}
			datalist.add(data);
			datamap.put(key, datalist);
		}
		
		//循环docname调用u9接口
		List<JSONObject> retlist=new ArrayList<JSONObject>();
		for(String key:datamap.keySet()) {
			String url=null;
			if(urls.containsKey(key))
				url=StrUtil.toString(urls.get(key));
			List<Map<String,Object>> datalist=(List<Map<String,Object>>)datamap.get(key);
			List<JSONObject> ret = u9rest.entitySave(context, url, datalist, entityInfo);
			retlist.addAll(ret);
		}
		
		return retlist;
	}

	/**
	 * OA单据批量回写清空U9字段
	 * @param datas 返回的数据
	 * @param tables 要更改的表名
	 * @param db 数据库连接
	 * @return
	 */
	@ServiceLog("u9单据批量回写清空字段")
	public int batchReWrite(List<Map<String, Object>> datas,Map<String,Object> tables,JSONObject db) throws
			Exception {
		Map<String,Object> datamap=new LinkedHashMap<String,Object>();
		//依据docname分组
		for(Map<String,Object> data:datas) {
			String docname=StrUtil.toString(data.get("DocName"));
			String status=StrUtil.toString(data.get("DocStatus"));
			String key=docname+"-"+status;

			List<Map<String,Object>> datalist=null;
			if(datamap.containsKey(key)) {
				datalist=(List<Map<String,Object>>)datamap.get(key);
			}else {
				datalist=new ArrayList<Map<String,Object>>();
			}
			datalist.add(data);
			datamap.put(key, datalist);
		}
		//循环调用U9的表
		List<JSONObject> retlist=new ArrayList<JSONObject>();
		int num = 0;
		int nu=0;
		for(String key:datamap.keySet()) {
			String tablename=null;
			if(tables.containsKey(key))
				tablename=StrUtil.toString(tables.get(key));
			List<Map<String,Object>> datalist=(List<Map<String,Object>>)datamap.get(key);
//			for (Map<String,Object> data:datas) {
//				String DocNo = StrUtil.toString(data.get("DocNo"));
			if(tablename.equals("AR_ARAPCAHead")){
				List<Map<String,Object>> newlist=new ArrayList<>();
				for(Map<String,Object> item:datalist){
					Map<String,Object> newitem=new LinkedHashMap<>();
					for(String key1:item.keySet()){
						if(key1.equals("DocNo"))
							newitem.put("DocNoForUI",item.get("DocNo"));
						else
							newitem.put(key1,item.get(key1));
					}
					newlist.add(newitem);
				}
				nu=sqlService.mapUpdate(newlist,db,tablename,"DocNoForUI","DescFlexField_PubDescSeg6",null);
			}else{
				nu = sqlService.mapUpdate(datalist, db, tablename, "DocNo", "DescFlexField_PubDescSeg6", null);
			}

			num = nu;
//			}
		}
		return num;
	}

	
	/**
	 * 将U9附件信息转换为H5格式
	 * @param datas u9返回附件信息
	 * @param codeField u9单据业务主键
	 * @param attField u9单据H5存储字段
	 * @return
	 */
	@ServiceLog("u9单据附件地址更新")
	public List<Map<String,Object>> getAttachmentH5(List<Map<String,Object>> datas,String u9host,String codeField,String attField){
		Map<String,Object> doclist=new LinkedHashMap<String,Object>();
		
//		附件信息按照单号分组
		for(Map<String,Object> data:datas) {
			if(data.get("EntityCode")==null) continue;
			
			String docno=data.get("EntityCode").toString();
			
			if(doclist.containsKey(docno)) {
				List<Object> attachments = (List<Object>)doclist.get(docno);
				attachments.add(data);
			}else {
				List<Object> attachments = new ArrayList<Object>();
				attachments.add(data);
				doclist.put(docno, attachments);
			}
		}
		
//		增对每张单构造附件H5
		List<Map<String,Object>> dochtmls=new ArrayList<Map<String,Object>>();
		for(String key:doclist.keySet()) {
			List<Object> attachments = (List<Object>)doclist.get(key);
			StringBuilder htmls=new StringBuilder();
			
			String docno=key;
			for(Object obj:attachments) {
				Map<String,Object> attachment=(Map<String,Object>)obj;
				String url=attachment.get("Url").toString();
				String filename=attachment.get("FileName").toString();
				
				String html="<p><a href=\"%s\" target=\"_blank\">%s</a></p>";
				html=String.format(html, u9host+url,filename);
				htmls.append(html);
			}
			htmls.append("<p>附件点击打开</p>");
			
			Map<String,Object> dochtml=new LinkedHashMap<String,Object>();
			dochtml.put(codeField, docno);
			dochtml.put(attField, htmls.toString());
			dochtmls.add(dochtml);
		}
		
		return dochtmls;
	}
}
